package com.learn.rule;


import com.learn.rule.model.CalculateResult;
import com.learn.rule.model.Message;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.api.definition.KiePackage;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;

import java.math.BigDecimal;
import java.util.*;


public class Application {

    public static void test1() {

        KieSession kieSession = getSession(getRule(1));

        CalculateResult calculateResult = new CalculateResult();
        kieSession.setGlobal("cResult", calculateResult);
        Message message = new Message();
        message.setA(new BigDecimal(3));
        message.setB(new BigDecimal(4));
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        map.put("a", new BigDecimal(1.27));
        map.put("b", new BigDecimal(1.33));
        //kieSession.insert(message);
        kieSession.insert(map);
        kieSession.fireAllRules();
        System.out.println("计算结果:" + calculateResult.getData());
        double a = 1.27;
        double b = 1.33;
        System.out.println("计算结果:" + a * b);
        BigDecimal big_a = new BigDecimal(a);
        BigDecimal big_b = new BigDecimal(b);
        System.out.println("计算结果:" + big_a.multiply(big_b));
    }


    public static void test2() {

        KieSession kieSession = getSession(getRule(2));

        CalculateResult calculateResult = new CalculateResult();
        kieSession.setGlobal("cResult", calculateResult);
        Message message = new Message();
        message.setA(new BigDecimal(1.27));
        message.setB(new BigDecimal(1.33));
        kieSession.insert(message);
        kieSession.fireAllRules();
        System.out.println("计算结果:" + calculateResult.getData());
        double a = 1.27;
        double b = 1.33;
        System.out.println("计算结果:" + a * b);
        BigDecimal big_a = new BigDecimal(a);
        BigDecimal big_b = new BigDecimal(b);
        System.out.println("计算结果:" + big_a.multiply(big_b));
    }

    public static KieSession getSession(String rule) {
        KieSession kieSession = null;
        try {
            KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            builder.add(ResourceFactory.newByteArrayResource(rule.getBytes("UTF-8")),ResourceType.DRL);
            InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
            Collection<KiePackage> packages = builder.getKnowledgePackages();
            knowledgeBase.addPackages(packages);
            kieSession = knowledgeBase.newKieSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kieSession;
    }

    public static String getRule(int id) {
        StringBuffer ruleSb = new StringBuffer();
        ruleSb.append("package com.learn.rule \n");
        ruleSb.append("dialect \"mvel\" \n");
        if (id == 1) {
            ruleSb.append("import java.util.Map; \n");
            ruleSb.append("global com.learn.rule.model.CalculateResult cResult; \n");
            ruleSb.append("rule \"rule1\" \n");
            ruleSb.append("when \n");
            ruleSb.append("$map : Map() \n");
            ruleSb.append("then \n");
            ruleSb.append("cResult.setData($map.get(\"a\") * $map.get(\"b\")); \n");
        }
        if (id == 2) {
            ruleSb.append("import com.learn.rule.model.Message; \n");
            ruleSb.append("global com.learn.rule.model.CalculateResult cResult; \n");
            ruleSb.append("rule \"rule2\" \n");
            ruleSb.append("when \n");
            ruleSb.append("$message : Message($a : a, $b : b) \n");
            ruleSb.append("then \n");
            ruleSb.append("cResult.setData($a.multiply($b)); \n");
        }
        ruleSb.append("end");
        return ruleSb.toString();
    }

    public static void main(String[] args) {
        test1();
        test2();
    }
}
